﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wicresoft.UnifyShow.SMSServices.Task
{
    public class CreateTask: IJob
    {
        string insertTable = @"
                INSERT INTO SM_SENDSMG
(SMGID,SRCNUM,DSTNUM,MSGSTR,SENDTIME,SENDUSER,SDUSRNAME,SDUSERTYPE,STATUS,RT_CODE,RECV_PRECHR,VERIFY_FLAG)
SELECT SUBMITTESTSEQ.NEXTVAL,'76249',:num,
:msg, SYSDATE,'ITIL','ITIL短信','1', 0,'9999','ITIL','1' FROM dual;

                            ";

        public void Execute(IJobExecutionContext context)
        {
            var day = ConfigurationSettings.AppSettings["AfterDay"];
            Logger.Log("start Create Task");
            string strSql = string.Empty;

            using (var trans = oda.BeginTransaction())
            {
                try
                {
                  
                }
                catch (Exception ex)
                {
                    Logger.Error(strSql);
                    Logger.Error("Create Task", ex);
                    trans.Rollback();
                }
            }
            try
            {
                ModelHelper<TaskModel>.CreateModel_By_DirectSql(@"
                    update  Task set Overflag=1
                    Where Status=1 And Overflag=0  And  To_Char(Enddate+1,'YYYY/MM/DD')<= To_Char(Sysdate,'YYYY/MM/DD')", oda);
            }
            catch (Exception ex)
            {
                Logger.Error("过期:", ex);
            }

            try
            {
                strSql = string.Format(@"
                    update Task set status=4,updatetime=sysdate    Where Jobtype=1 And Status=2
                    and to_char(completetime+{0},'YYYYMMDD')<=to_char(sysdate,'YYYYMMDD')", day);
                ModelHelper<TaskModel>.CreateModel_By_DirectSql(strSql, oda);
            }
            catch (Exception ex)
            {
                Logger.Error(strSql);
                Logger.Error("日常巡检任务，在已完成之后三天自动关闭:", ex);
            }
        }
    }
}
